package com.example.ch04;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private Button btn_add,btn_show,btn_delete;
    private ListView lv;
    Create_Datebase cdatebase;
    SQLiteDatabase db;
    private TextView tv_name,tv_sex,tv_phone,tv_workplace,tv_work;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_show=(Button)findViewById(R.id.btn_show);
        btn_add=(Button)findViewById(R.id.btn_add);
        btn_delete=(Button)findViewById(R.id.btn_delete);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump();
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,get(),R.layout.id_listview,new String[]{"name","sex","phone","workplace","work"},
                        new int[]{R.id.tv_name,R.id.tv_sex,R.id.tv_phone,R.id.tv_workplace,R.id.tv_work});
                lv=(ListView)findViewById(R.id.lv);
                lv.setAdapter(adapter);
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });

    }
    //删除数据
    public void delete(){
        cdatebase=new Create_Datebase(this);
        db=cdatebase.getWritableDatabase();
        db.delete("information",null,null);
        Toast.makeText(this,"信息已经删除",Toast.LENGTH_SHORT).show();
        final SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,get(),R.layout.id_listview,new String[]{"name","sex","phone","workplace","work"},
                new int[]{R.id.tv_name,R.id.tv_sex,R.id.tv_phone,R.id.tv_workplace,R.id.tv_work});
        lv=(ListView)findViewById(R.id.lv);
        lv.setAdapter(adapter);
        db.close();
    }

    //查询数据
    public List<Map<String,Object>> get(){
        List<Map<String,Object>>list=new ArrayList<Map<String, Object>>();//所有列表记录
        Map<String,Object> map=new HashMap<String, Object>();//列表项对应的一条记录
        //创建数据库对象
        cdatebase=new Create_Datebase(this);
        db=cdatebase.getReadableDatabase();
        Cursor cursor=db.query("information",null,null,null,null,null,null);
        if(cursor.getCount()==0){
            Toast.makeText(MainActivity.this,"没有数据",Toast.LENGTH_SHORT).show();
        }else {
            while(cursor.moveToNext()){
                Toast.makeText(MainActivity.this,"正在查询",Toast.LENGTH_SHORT).show();
                for(int i=0;i<cursor.getCount();i++){
                    cursor.moveToPosition(i);
                    String name=cursor.getString(0);
                    String sex=cursor.getString(1);
                    String phone=cursor.getString(2);
                    String workplace=cursor.getString(3);
                    String work=cursor.getString(4);
                    map=new HashMap<String,Object>();
                    map.put("name",name);
                    map.put("sex",sex);
                    map.put("phone",phone);
                    map.put("workplace",workplace);
                    map.put("work",work);
                    list.add(map);
                }
        }
            cursor.close();
            db.close();
            Log.i("a","查询成功");
        }
        return list;
    }
    //点击添加名片跳转到添加的页面
    public void jump(){
        Intent intent=new Intent(this,add_activity.class);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            if(requestCode==1){
                if(resultCode==1){
                }
            }
        }
    }

//    class MyBaseAdapter extends BaseAdapter{
//        @Override
//        public int getCount() {
//            return names.length;
//        }
//
//        @Override
//        public long getItemId(int position) {
//            return 0;
//        }
//
//        @Override
//        public Object getItem(int position) {
//            return null;
//        }
//
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            View view=View.inflate(MainActivity.this,R.layout.id_listview,null);
//
//            tv_name=(TextView)view.findViewById(R.id.tv_name);
//            tv_sex=(TextView)view.findViewById(R.id.tv_sex);
//            tv_phone=(TextView)view.findViewById(R.id.tv_phone);
//            tv_workplace=(TextView)view.findViewById(R.id.tv_workplace);
//            tv_work=(TextView)view.findViewById(R.id.tv_work);
//
//            Person person=(Person)get();
//
////                tv_name.setText();
//
//            return view;
//        }
//    }


}











